﻿using System;
using System.Data.Common;

namespace Centido.Management.ConsoleManager
{
    /// <summary>
    ///   A console command that adds the specified security role into another security role.
    /// </summary>
    internal class AddRoleInRoleCommand : Command
    {
        /// <summary>
        ///   The role name parameter.
        /// </summary>
        public const string RoleNameParameter = "role";

        /// <summary>
        ///   The role name parameter (short name).
        /// </summary>
        public const string RoleNameShortParameter = "r";

        /// <summary>
        ///   The parent role name parameter.
        /// </summary>
        public const string ParentRoleNameParameter = "parentrole";

        /// <summary>
        ///   The parent role name parameter (short name).
        /// </summary>
        public const string ParentRoleNameShortParameter = "pr";


        /// <summary>
        ///   Gets the console command name.
        /// </summary>
        public override string Name
        {
            get { return "addroleinrole"; }
        }


        /// <summary>
        ///   Gets the console command description.
        /// </summary>
        public override string Description
        {
            get { return Messages.AddRoleInRoleCommandDescription; }
        }


        /// <summary>
        ///   Gets the console command full description.
        /// </summary>
        public override string FullDescription
        {
            get { return Messages.AddRoleInRoleCommandFullDescription; }
        }


        /// <summary>
        ///   Executes the console command.
        /// </summary>
        /// <param name="context">The console command execution context.</param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="context"/> is null.</exception>
        public override void Execute(CommandExecutionContext context)
        {
            base.Execute(context);

            // If not all required parameters were specified
            if (!context.ParameterValueIsNotEmpty(RoleNameParameter, RoleNameShortParameter)
                || !context.ParameterValueIsNotEmpty(ParentRoleNameParameter, ParentRoleNameShortParameter))
            {
                Console.WriteLine(Messages.AddRoleInRoleCommandNotAllParametersSpecifiedError);
                return;
            }

            var domain = context.CurrentDomain;

            if (domain == null) return;

            // If the current user has no permission to perform this command
            if (!domain.CurrentUser.CanChangeSecurity)
            {
                Console.WriteLine(Messages.NeedCanChangeSecurityPermissionMessage);
                return;
            }

            // The role name
            var roleName = context.GetParameterValue(RoleNameParameter, RoleNameShortParameter);

            // The parent role name
            var parentRoleName = context.GetParameterValue(ParentRoleNameParameter, ParentRoleNameShortParameter);

            try
            {
                // Trying to find the role
                var role = domain.GetRole(roleName);

                // If cannot find the role
                if (role == null)
                {
                    Console.WriteLine(Messages.CannotFindRoleError, roleName);
                    return;
                }

                // Trying to find the parent role
                var parentRole = domain.GetRole(parentRoleName);

                // If cannot find the parent role
                if (parentRole == null)
                {
                    Console.WriteLine(Messages.CannotFindRoleError, parentRoleName);
                    return;
                }

                if (!parentRole.ContainsRole(role))
                    parentRole.AddRole(role);
            }
            catch (DbException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine();
                return;
            }

            // Displaying the success message
            Console.WriteLine(Messages.CommandCompletedMessage);
        }
    }
}
